package com.se7en.ufutil;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

	


public class StudyArea extends BalloonItemizedOverlay<OverlayItem>
{
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private StudyAreaModel[] studyLocations;
	DataAccess dataAccess;
	MapView mapView;
	
	public StudyArea(Drawable defaultMarker, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
	}
	
	public StudyArea(Drawable defaultMarker, Context context, DataAccess dataAccess, MapView mapView) {
		super(defaultMarker, mapView);
		this.mContext = context;
		this.dataAccess = dataAccess;
		studyLocations = this.dataAccess.getStudyLocs();
		OverlayItem temp;
		Drawable icon = context.getResources().getDrawable(R.drawable.study_marker);
		
		if(studyLocations.length > 1) {
			for (StudyAreaModel stud : studyLocations)
			{
				
				temp = new OverlayItem(stud.location, stud.name , stud.hours + "\n" + "Click here for more information.");
				temp.setMarker(icon);
				icon.setBounds(-icon.getIntrinsicWidth()/2, -icon.getIntrinsicHeight(), icon.getIntrinsicWidth() /2, 0);
				this.addOverlay(temp);
			}
		}
	}

	
	// This function is responsible for drawing the overlay itself
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
		super.draw(canvas, mapView, shadow);
		return;
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		Intent i = new Intent(mContext, StudyDisplay.class);
		i.putExtra("name", item.getTitle());
		mContext.startActivity(i);
		return true;
		}
}
